package org.zfes.snowier.cloud.zbss.widget.act.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.data.Record;
import org.zfes.snowier.core.exceptions.ServiceLogicalException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;

import com.google.common.collect.Lists;

import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActQuestionService;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActSubjectQuesAnswerService;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActSubjectQuestionsService;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActSubjectService;
import org.zfes.snowier.cloud.zbss.widget.act.dao.ActSubjectQuesAnswerMapper;
import org.zfes.snowier.cloud.zbss.widget.act.dto.ActSubjectQuesAnsweDto;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActQuestion;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActSubject;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActSubjectQuesAnswer;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActSubjectQuestions;

import java.util.stream.Stream;
@Service
public class ActSubjectQuesAnswerServiceImpl implements IActSubjectQuesAnswerService{
 	@Autowired
	private ActSubjectQuesAnswerMapper actAnswerMapper;
 	@Autowired
	private IActQuestionService actQuestionService;
 	
 	@Autowired
	private IActSubjectQuestionsService actSubjectQuestionsService;
 	@Autowired
	private IActSubjectService actSubjectService;
	private Byte geAnswerResult(ActQuestion actQuestion,ActSubjectQuesAnswer answer) {
//		String answerDxMxPd= actQuestion.getAnswerDxMxPd();
//		String answerTk= actQuestion.getAnswerTk();
//		String answerJdLsFx= actQuestion.getAnswerJdLsFx();
		boolean res=actQuestion.getAnswerDxMxPd().equals(answer.getAnswerDxMxPd());
		//#0 未答题1 正确 2 错误#
		return res?Byte.valueOf("1"):Byte.valueOf("2");
	}
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createActSubjectProcessAnswer(ActSubjectQuesAnswer answer) {
		ZAssert.notNull(answer.getSubjectId(), "subjectId 参数错误");
		ZAssert.notNull(answer.getQuesId(), "QuesId 参数错误");
		
		ActSubjectQuestions subjectQues=actSubjectQuestionsService.loadActSubjectQuestionsById(answer.getSubjectQuesId()).orElseThrow(()->new ServiceLogicalException("课堂问题查询错误"));
		
		answer.setQuesId(subjectQues.getQuesId()).setSubjectId(subjectQues.getSubjectId());
		
		
		ActQuestion actQuestion=actQuestionService.loadActQuestionById(answer.getQuesId()).orElseThrow(()->new ServiceLogicalException("问题查询错误"));

		ActSubjectQuesAnswer actAnswerInDB= actAnswerMapper.selectQuetionAnswer(answer.getSubjectId(),answer.getQuesId(),answer.getAnswerUserId());
		if(actAnswerInDB!=null) {
			actAnswerInDB
					.setAnswerDxMxPd(answer.getAnswerDxMxPd())
					.setAnswerJdLsFx(answer.getAnswerJdLsFx())
					.setAnswerTk(answer.getAnswerTk())
					.setAnswerTime(ZDateUtil.newDate());
			
			actAnswerInDB.setAnswerResult(geAnswerResult( actQuestion, actAnswerInDB));
			
			ZBeanUtil.validateBean(actAnswerInDB);
			
			actAnswerMapper.insertSelective(actAnswerInDB);
		}else {
			answer.setAnswerTime(ZDateUtil.newDate());
			
			answer.setAnswerResult(geAnswerResult( actQuestion, answer));
			
			ZBeanUtil.validateBean(answer);
			
			actAnswerMapper.insertSelective(answer);

		}
		
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ActSubjectQuesAnswer> loadActSubjectProcessAnswerById(Long id) {
		if(id!=null){
			return Optional.ofNullable(actAnswerMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<ActSubjectQuesAnswer> loadAnswerBySubjectQuesId(Long subjectQuesId) {
		if(subjectQuesId!=null){
			return actAnswerMapper.selectBySubjectQuesId(subjectQuesId);
		}
		return Lists.newArrayList();
		
	}
	
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteActSubjectProcessAnswer(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		actAnswerMapper.deleteByPrimaryKey(id);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setActSubjectProcessAnswerStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		actAnswerMapper.updateStatus( id,  status) ;
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadActSubjectProcessAnswerDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(actAnswerMapper.selectListCount(pm), actAnswerMapper.selectMapListPage(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Record> loadAnswerDistribution(Long subjectId,Long quesId) {
		
		return Optional.ofNullable(actAnswerMapper.selectAnswerDistribution(subjectId,quesId));
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public Optional<ActQuestion>  loadOpenAnswer(Long id) {
		ActSubjectQuesAnswer answer=loadActSubjectProcessAnswerById(id).orElseThrow(()->new ServiceLogicalException("课堂问题查询错误"));
		ZAssert.equals(answer.getOpenAnswer(), Byte.valueOf("2"), "答案未公布");
		
		ActQuestion actQuestion=actQuestionService.loadActQuestionById(answer.getQuesId())
			.orElseThrow(()->new ServiceLogicalException("课堂问题查询错误"));
		return Optional.of(actQuestion);
	}

	@Override
	public Optional<ActSubjectQuesAnsweDto> loadActSubjectQuesAnsweDto(Long subjectQuesId) {
		
		ActSubjectQuestions subjectQues = actSubjectQuestionsService
						.loadActSubjectQuestionsById(subjectQuesId)
						.orElseThrow(()->new ServiceLogicalException("问题查询错误"));
		
		ActQuestion actQuestion=actQuestionService
						.loadActQuestionById(subjectQues.getQuesId())
						.orElseThrow(()->new ServiceLogicalException("课堂问题查询错误"));
		
		ActSubject actSubject=actSubjectService
				.loadActSubjectById(subjectQues.getSubjectId())
				.orElseThrow(()->new ServiceLogicalException("未查询到数据"));
		List<ActSubjectQuesAnswer> subjectQuesAnswerList= loadAnswerBySubjectQuesId(subjectQuesId);
		
		
		return Optional.of(new ActSubjectQuesAnsweDto()
							.setActQuestion(actQuestion)
							.setActSubject(actSubject)
							.setActSubjectQuesAnswerList(subjectQuesAnswerList));
	}
	
}